package com.example.associationmanager.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.associationmanager.bean.User;
import com.example.associationmanager.service.UserService;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
@Log4j2
@Controller
public class indexController {//登录控制
    @Autowired
    UserService userService;
    @GetMapping(value = {"/","/login"})//跳转到登录页面
    public Object loginPage(){
        return "page/login-3.html";
    }


    @PostMapping(value = "/user/login")//登录表单提交
    public String login(Model model, String userAccountNumber, String userPassword, Integer userAuth, HttpServletRequest request){
        HttpSession session = request.getSession();
        log.info("接收到的参数"+userAccountNumber+" "+userPassword+" "+userAuth+" ");
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>().eq("user_account_number",userAccountNumber);
        User userServiceOne = userService.getOne(queryWrapper);//根据用户账号查询用户信息

        if(userServiceOne != null){//数据库中是否存在此账号的数据
            if(userServiceOne.getUserAccountNumber().equals(userAccountNumber)//用户名，密码，权限均验证通过
                    &&userServiceOne.getUserPassword().equals(userPassword)
                    &&userServiceOne.getUserAuth().equals(userAuth)
                    ){
//                Constants.KAPTCHA_SESSION_KEY

                log.info("登录成功");
                //把登录成功的用户保存起来
                session.setAttribute("loginUser",userServiceOne);//保存登录的用户信息
                //登录成功，重定向;防止表单重复提交使用重定向
                return "redirect:/index";
            }else if(!userServiceOne.getUserAccountNumber().equals(userAccountNumber)//账号或者密码未通过验证
                    ||!userServiceOne.getUserPassword().equals(userPassword)){
                model.addAttribute("msg","账号或密码存在错误，请检查重试！");
                return "page/login-3.html";
            }else {
                model.addAttribute("msg","权限选择错误，请检查重试！");
                return "page/login-3.html";
            }

        }else {//数据库中无此账号数据
            model.addAttribute("msg","此账号不存在，请检查重试！");
            log.info("登录时：\t"+"用户不存在");
            //回到登录页
            return "page/login-3.html";
        }

    }

    @ResponseBody
    @GetMapping("/init/json")//根据用户权限，动态加载页面配置文件
    public Object getInitJson(HttpSession session) {
        User user =(User) session.getAttribute("loginUser");
        if(user.getUserAuth().equals(1)){//权限判断
            String json="{\n" +
                    "  \"homeInfo\": {\n" +
                    "    \"title\": \"首页\",\n" +
                    "    \"href\": \"page/welcome\"\n" +
                    "  },\n" +
                    "  \"logoInfo\": {\n" +
                    "    \"title\": \"智慧社团\",\n" +
                    "    \"image\": \"images/logo.png\",\n" +
                    "    \"href\": \"\"\n" +
                    "  },\n" +
                    "  \"menuInfo\": [\n" +
                    "    {\n" +
                    "      \"title\": \"常规管理\",\n" +
                    "      \"icon\": \"fa fa-address-book\",\n" +
                    "      \"href\": \"http://www.baidu.com\",\n" +
                    "      \"target\": \"_self\",\n" +
                    "      \"child\": [\n" +
                    "        {\n" +
                    "          \"title\": \"主页\",\n" +
                    "          \"href\": \"\",\n" +
                    "          \"icon\": \"fa fa-home\",\n" +
                    "          \"target\": \"_self\",\n" +
                    "          \"child\": [\n" +
                    "            {\n" +
                    "              \"title\": \"首页\",\n" +
                    "              \"href\": \"page/welcome\",\n" +
                    "              \"icon\": \"fa fa-tachometer\",\n" +
                    "              \"target\": \"_self\"\n" +
                    "            }\n" +
                    "    \n" +
                    "          ]\n" +
                    "        },\n" +
                    " \n" +
                    "    {\n" +
                    "      \"title\": \"现有社团信息\",\n" +
                    "      \"href\": \"page/lable_brief\",\n" +
                    "      \"icon\": \"fa fa-file-text\",\n" +
                    "      \"target\": \"_self\"\n" +
                    "      \n" +
                    "    },\n" +
                    "\n" +
                    "        {\n" +
                    "          \"title\": \"社团管理\",\n" +
                    "          \"icon\": \"fa fa-align-left\",\n" +
                    "          \"target\": \"_self\",\n" +
                    "\t\t  \"child\": [\n" +
                    "            {\n" +
                    "              \"title\": \"社团信息管理\",\n" +
                    "              \"href\": \"page/table_community\",\n" +
                    "              \"icon\": \"fa fa-building\",\n" +
                    "              \"target\": \"_self\"\n" +
                    "            },\n" +
                    "            {\n" +
                    "              \"title\": \"社长管理\",\n" +
                    "              \"href\": \"page/table_user\",\n" +
                    "              \"icon\": \"fa fa-gears\",\n" +
                    "              \"target\": \"_self\"\n" +
                    "            }\n" +
                    "\t\t  \n" +
                    "\t\t  ]\n" +
                    "        },\n" +
                    "\t\t{\n" +
                    "\t\t  \"title\": \"系统设置\",\n" +
                    "\t\t  \"href\": \"page/setting\",\n" +
                    "\t\t  \"icon\": \"fa fa-gears\",\n" +
                    "\t\t  \"target\": \"_self\"\n" +
                    "\t\t}\n" +
                    "\n" +
                    "\n" +
                    "      ]\n" +
                    "    }\n" +
                    "  ]\n" +
                    "}";
            log.debug("权限：超级管理员登录");
            return json;
        }else if(user.getUserAuth().equals(2)){
            String json = "{\n" +
                    "  \"homeInfo\": {\n" +
                    "    \"title\": \"首页\",\n" +
                    "    \"href\": \"page/welcome\"\n" +
                    "  },\n" +
                    "  \"logoInfo\": {\n" +
                    "    \"title\": \"智慧社团\",\n" +
                    "    \"image\": \"images/logo.png\",\n" +
                    "    \"href\": \"\"\n" +
                    "  },\n" +
                    "  \"menuInfo\": [\n" +
                    "    {\n" +
                    "      \"title\": \"常规管理\",\n" +
                    "      \"icon\": \"fa fa-address-book\",\n" +
                    "      \"href\": \"http://www.baidu.com\",\n" +
                    "      \"target\": \"_self\",\n" +
                    "      \"child\": [\n" +
                    "        {\n" +
                    "          \"title\": \"主页\",\n" +
                    "          \"href\": \"\",\n" +
                    "          \"icon\": \"fa fa-home\",\n" +
                    "          \"target\": \"_self\",\n" +
                    "          \"child\": [\n" +
                    "            {\n" +
                    "              \"title\": \"首页\",\n" +
                    "              \"href\": \"page/welcome\",\n" +
                    "              \"icon\": \"fa fa-tachometer\",\n" +
                    "              \"target\": \"_self\"\n" +
                    "            }\n" +
                    "    \n" +
                    "          ]\n" +
                    "        },\n" +
                    "\n" +
                    "\t{\n" +
                    "\t  \"title\": \"社团信息\",\n" +
                    "\t  \"href\": \"/page/welcome-2\",\n" +
                    "\t  \"icon\": \"fa fa-reorder\",\n" +
                    "\t  \"target\": \"_self\"\n" +
                    "\t  \n" +
                    "\t},\n" +
                    "\n" +
                    "\t{\n" +
                    "\t  \"title\": \"审批\",\n" +
                    "\t  \"href\": \"page/table_examine\",\n" +
                    "\t  \"icon\": \"fa fa-connectdevelop\",\n" +
                    "\t  \"target\": \"_self\"\n" +
                    "\t  \n" +
                    "\t},\n" +
                    "\n" +
                    "        {\n" +
                    "          \"title\": \"社团管理\",\n" +
                    "          \"icon\": \"fa fa-align-left\",\n" +
                    "          \"target\": \"_self\",\n" +
                    "\t\t  \"child\": [\n" +
                    "\t\t    {\n" +
                    "\t\t      \"title\": \"财务管理\",\n" +
                    "\t\t      \"href\": \"page/table_funds\",\n" +
                    "\t\t      \"icon\": \"fa fa-list-alt\",\n" +
                    "\t\t      \"target\": \"_self\"\n" +
                    "\t\t    },\n" +
                    "\t\t\t{\n" +
                    "\t\t\t  \"title\": \"活动管理\",\n" +
                    "\t\t\t  \"href\": \"page/table_activity\",\n" +
                    "\t\t\t  \"icon\": \"fa fa-columns\",\n" +
                    "\t\t\t  \"target\": \"_self\"\n" +
                    "\t\t\t},\n" +
                    "\t\t\t{\n" +
                    "\t\t\t  \"title\": \"人员管理\",\n" +
                    "\t\t\t  \"href\": \"page/table\",\n" +
                    "\t\t\t  \"icon\": \"fa fa-table\",\n" +
                    "\t\t\t  \"target\": \"_self\"\n" +
                    "\t\t\t},\n" +
                    "\t\t  {\n" +
                    "\t\t\t  \"title\": \"普通用户管理\",\n" +
                    "\t\t\t  \"href\": \"page/table_user\",\n" +
                    "\t\t\t  \"icon\": \"fa fa-gears\",\n" +
                    "\t\t\t  \"target\": \"_self\"\n" +
                    "\t\t  },\n" +
                    "\t\t\t{\n" +
                    "\t\t\t  \"title\": \"资产状况\",\n" +
                    "\t\t\t  \"href\": \"page/lable_property\",\n" +
                    "\t\t\t  \"icon\": \"fa fa-th\",\n" +
                    "\t\t\t  \"target\": \"_self\"\n" +
                    "\t\t\t}\n" +
                    "\t\t\t\n" +
                    "\t\t  \n" +
                    "\t\t  ]\n" +
                    "        },\n" +
                    "\n" +
                    "\t\t{\n" +
                    "\t\t  \"title\": \"系统设置\",\n" +
                    "\t\t  \"href\": \"page/setting\",\n" +
                    "\t\t  \"icon\": \"fa fa-gears\",\n" +
                    "\t\t  \"target\": \"_self\"\n" +
                    "\t\t}\n" +
                    "\n" +
                    "      ]\n" +
                    "    }\n" +
                    "  ]\n" +
                    "}";
            log.debug("权限：普通管理员登录");
            return json;
        }else {
            String json="{\n" +
                    "  \"homeInfo\": {\n" +
                    "    \"title\": \"首页\",\n" +
                    "    \"href\": \"page/welcome\"\n" +
                    "  },\n" +
                    "  \"logoInfo\": {\n" +
                    "    \"title\": \"智慧社团\",\n" +
                    "    \"image\": \"images/logo.png\",\n" +
                    "    \"href\": \"\"\n" +
                    "  },\n" +
                    "  \"menuInfo\": [\n" +
                    "    {\n" +
                    "      \"title\": \"常规管理\",\n" +
                    "      \"icon\": \"fa fa-address-book\",\n" +
                    "      \"href\": \"http://www.baidu.com\",\n" +
                    "      \"target\": \"_self\",\n" +
                    "      \"child\": [\n" +
                    "        {\n" +
                    "          \"title\": \"主页\",\n" +
                    "          \"href\": \"\",\n" +
                    "          \"icon\": \"fa fa-home\",\n" +
                    "          \"target\": \"_self\",\n" +
                    "          \"child\": [\n" +
                    "            {\n" +
                    "              \"title\": \"首页\",\n" +
                    "              \"href\": \"page/welcome\",\n" +
                    "              \"icon\": \"fa fa-tachometer\",\n" +
                    "              \"target\": \"_self\"\n" +
                    "            }\n" +
                    "    \n" +
                    "          ]\n" +
                    "        },\n" +
                    " \n" +
                    "    {\n" +
                    "      \"title\": \"社团简介\",\n" +
                    "      \"href\": \"page/lable_brief\",\n" +
                    "      \"icon\": \"fa fa-file-text\",\n" +
                    "      \"target\": \"_self\"\n" +
                    "\n" +
                    "    },\n" +
                    "\n" +
                    "        {\n" +
                    "          \"title\": \"社团事务\",\n" +
                    "          \"icon\": \"fa fa-sliders\",\n" +
                    "          \"target\": \"_self\",\n" +
                    "\t\t  \"child\": [\n" +
                    "            {\n" +
                    "              \"title\": \"社团操作\",\n" +
                    "              \"href\": \"page/table_community_show\",\n" +
                    "              \"icon\": \"fa fa-columns\",\n" +
                    "              \"target\": \"_self\"\n" +
                    "            },\n" +
                    "\t\t    {\n" +
                    "\t\t      \"title\": \"活动查看与申请\",\n" +
                    "\t\t      \"href\": \"page/table_activity_show\",\n" +
                    "\t\t      \"icon\": \"fa fa-list-alt\",\n" +
                    "\t\t      \"target\": \"_self\"\n" +
                    "\t\t    },\n" +
                    "\t\t\t{\n" +
                    "\t\t\t  \"title\": \"经费查看与申请\",\n" +
                    "\t\t\t  \"href\": \"page/table_finance\",\n" +
                    "\t\t\t  \"icon\": \"fa fa-columns\",\n" +
                    "\t\t\t  \"target\": \"_self\"\n" +
                    "\t\t\t},\n" +
                    "\t\t\t{\n" +
                    "\t\t\t  \"title\": \"同社成员查看\",\n" +
                    "\t\t\t  \"href\": \"page/table_show\",\n" +
                    "\t\t\t  \"icon\": \"fa fa-table\",\n" +
                    "\t\t\t  \"target\": \"_self\"\n" +
                    "\t\t\t}\n" +
                    "\t\t  ]\n" +
                    "        },\n" +
                    "        {\n" +
                    "          \"title\": \"学习中心\",\n" +
                    "          \"icon\": \"fa fa-sliders\",\n" +
                    "          \"target\": \"_self\",\n" +
                    "          \"child\": [\n" +
                    "\n" +
                    "            {\n" +
                    "              \"title\": \"学习强国\",\n" +
                    "              \"href\": \"https://www.xuexi.cn/\",\n" +
                    "              \"icon\": \"fa fa-columns\",\n" +
                    "              \"target\": \"_self\"\n" +
                    "            },\n" +
                    "            {\n" +
                    "              \"title\": \"国家高等教育平台\",\n" +
                    "              \"href\": \"https://www.chinaooc.com.cn\",\n" +
                    "              \"icon\": \"fa fa-list-alt\",\n" +
                    "              \"target\": \"_self\"\n" +
                    "            },\n" +
                    "            {\n" +
                    "              \"title\": \"共青团中央\",\n" +
                    "              \"href\": \"http://www.cyol.com/\",\n" +
                    "              \"icon\": \"fa fa-columns\",\n" +
                    "              \"target\": \"_self\"\n" +
                    "            },\n" +
                    "            {\n" +
                    "              \"title\": \"新疆共青团\",\n" +
                    "              \"href\": \"http://zt.ts.cn/cms_udf/2019/tuanwei/index.shtml\",\n" +
                    "              \"icon\": \"fa fa-columns\",\n" +
                    "              \"target\": \"_self\"\n" +
                    "            },\n" +
                    "            {\n" +
                    "              \"title\": \"中国大学MOOC\",\n" +
                    "              \"href\": \"https://www.icourse163.org/\",\n" +
                    "              \"icon\": \"fa fa-columns\",\n" +
                    "              \"target\": \"_self\"\n" +
                    "            },\n" +
                    "            {\n" +
                    "              \"title\": \"豆瓣阅读\",\n" +
                    "              \"href\": \"https://read.douban.com/ebooks/\",\n" +
                    "              \"icon\": \"fa fa-columns\",\n" +
                    "              \"target\": \"_self\"\n" +
                    "            }\n" +
                    "\n" +
                    "          ]\n" +
                    "        },\n" +
                    "        {\n" +
                    "          \"title\": \"社员便捷服务\",\n" +
                    "          \"icon\": \"fa fa-sliders\",\n" +
                    "          \"target\": \"_self\",\n" +
                    "          \"child\": [\n" +
                    "            {\n" +
                    "              \"title\": \"社员综合服务中心\",\n" +
                    "              \"href\": \"http://116.62.169.52/\",\n" +
                    "              \"icon\": \"fa fa-columns\",\n" +
                    "              \"target\": \"_self\"\n" +
                    "            },\n" +
                    "            {\n" +
                    "              \"title\": \"今日校园\",\n" +
                    "              \"href\": \"https://www.campushoy.com/\",\n" +
                    "              \"icon\": \"fa fa-columns\",\n" +
                    "              \"target\": \"_self\"\n" +
                    "            },\n" +
                    "            {\n" +
                    "              \"title\": \"学习通\",\n" +
                    "              \"href\": \"https://xjkjxy.mh.chaoxing.com/\",\n" +
                    "              \"icon\": \"fa fa-list-alt\",\n" +
                    "              \"target\": \"_self\"\n" +
                    "            },\n" +
                    "            {\n" +
                    "              \"title\": \"有道词典\",\n" +
                    "              \"href\": \"https://m.youdao.com/m/\",\n" +
                    "              \"icon\": \"fa fa-columns\",\n" +
                    "              \"target\": \"_self\"\n" +
                    "            },\n" +
                    "            {\n" +
                    "              \"title\": \"果壳网\",\n" +
                    "              \"href\": \"https://www.guokr.com/\",\n" +
                    "              \"icon\": \"fa fa-columns\",\n" +
                    "              \"target\": \"_self\"\n" +
                    "            }\n" +
                    "\n" +
                    "          ]\n" +
                    "        },\n" +
                    "        {\n" +
                    "          \"title\": \"信息通知\",\n" +
                    "          \"icon\": \"fa fa-sliders\",\n" +
                    "          \"target\": \"_self\",\n" +
                    "          \"child\": [\n" +
                    "\n" +
                    "            {\n" +
                    "              \"title\": \"社团通知\",\n" +
                    "              \"href\": \"/page/info_inform\",\n" +
                    "              \"icon\": \"fa fa-columns\",\n" +
                    "              \"target\": \"_self\"\n" +
                    "            },\n" +
                    "            {\n" +
                    "              \"title\": \"信息收集\",\n" +
                    "              \"href\": \"https://wj.qq.com/login.html?login_type=phone\",\n" +
                    "              \"icon\": \"fa fa-list-alt\",\n" +
                    "              \"target\": \"_self\"\n" +
                    "            }\n" +
                    "\n" +
                    "          ]\n" +
                    "        },\n" +
                    "        {\n" +
                    "          \"title\": \"账号管理\",\n" +
                    "          \"icon\": \"fa fa-file-text\",\n" +
                    "          \"target\": \"_self\",\n" +
                    "          \"child\": [\n" +
                    "            {\n" +
                    "              \"title\": \"账号信息\",\n" +
                    "              \"href\": \"page/user-setting\",\n" +
                    "              \"icon\": \"fa fa-codepen\",\n" +
                    "              \"target\": \"_self\"\n" +
                    "            },\n" +
                    "            {\n" +
                    "              \"title\": \"密码修改\",\n" +
                    "              \"href\": \"page/user-password\",\n" +
                    "              \"icon\": \"fa fa-list-alt\",\n" +
                    "              \"target\": \"_self\"\n" +
                    "            }\n" +
                    "\n" +
                    "          ]\n" +
                    "        }\n" +
                    "\n" +
                    "      ]\n" +
                    "    }\n" +
                    "  ]\n" +
                    "}";
            log.debug("权限：普通用户登录");
            return json;
        }

    }
    @GetMapping("/user_logout")//用户退出登录
    public String logout(HttpSession session){
        session.invalidate();      //清空session
        log.info("用户退出登录");
        return "redirect:/login";  //重定向跳转到登录界面
    }
}
